package com.leyou.item.service;


import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dao.*;
import com.leyou.item.pojo.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author:yanjizhw@163.com
 * @date:2019-10-14 13:17
 * 描述:
 * @since:1.0.0
 */
@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleAclMapper roleAclMapper;

    @Autowired
    private AclMapper aclMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Autowired
    private ManagerMapper managerMapper;

    public List<Acl> roleTree() {

        return roleMapper.roleTree();
    }

    public List<Role> roleList() {

        return roleMapper.selectAll();
    }

    public List<Acl> getAcl(RoleAcl roleAcl) {

        Example example = new Example(RoleAcl.class);
        example.createCriteria().andEqualTo("role_id", roleAcl.getRole_id());
        List<RoleAcl> roleAcls = roleAclMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(roleAcls)){
            throw new LyException(ExceptionEnum.ROLE_ACL_NOT_FOUND);
        }

        List<Integer> idList = new ArrayList<>();
        for (RoleAcl r : roleAcls){
            idList.add(r.getAcl_id());
        }
        List<Acl> list = aclMapper.getByIdList(idList);
        return list;
    }

    public void addRoleAcl(RoleAcl roleAcl, String[] ids) {

        // 1、数组转集合
        List<String> acl_ids = new ArrayList<>();
        for (String id: ids){
            acl_ids.add(id);
        }

        Example example = new Example(RoleAcl.class);
        example.createCriteria().andEqualTo("role_id", roleAcl.getRole_id());
        List<RoleAcl> roleAcls = roleAclMapper.selectByExample(example);

        // 2、通过角色 id 获取权限点 id
        List<String> role_ids = new ArrayList<>();
        for (RoleAcl r: roleAcls){
            role_ids.add(Integer.toString(r.getAcl_id()));
        }

        // 3、去除相同的元素
        acl_ids.removeAll(role_ids);

        // 4、把角色 id 和 权限点 id 循环插入数据库
        for (int i=0; i<acl_ids.size(); i++){
            RoleAcl roleAcl1 = new RoleAcl();
            roleAcl1.setId(null);
            roleAcl1.setRole_id(roleAcl.getRole_id());
            roleAcl1.setAcl_id(Integer.valueOf(acl_ids.get(i)));
            roleAcl1.setOperator("admin");
            roleAcl1.setOperate_time(new Date());
            roleAcl1.setOperate_ip("127.0.0.1");

            roleAclMapper.insert(roleAcl1);
        }
    }

    public List<Manager> getUser(RoleUser roleUser) {

        Example example = new Example(RoleUser.class);
        example.createCriteria().andEqualTo("role_id", roleUser.getRole_id());
        List<RoleUser> roleUsers = roleUserMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(roleUsers)){
            throw new LyException(ExceptionEnum.ROLE_USER_NOT_FOUND);
        }

        List<Integer> idList = new ArrayList<>();
        for (RoleUser r : roleUsers){
            idList.add(r.getUser_id());
        }

        return managerMapper.getByIdList(idList);
    }

    public List<Manager> userTree() {

        Example example = new Example(Manager.class);
        example.createCriteria().andNotEqualTo("status", 2);
        return managerMapper.selectByExample(example);
    }

    public void addRoleUser(RoleAcl roleAcl, String[] ids) {
        // 1、数组转集合
        List<String> role_ids = new ArrayList<>();
        for (String id: ids){
            role_ids.add(id);
        }

        Example example = new Example(RoleAcl.class);
        example.createCriteria().andEqualTo("role_id", roleAcl.getRole_id());
        List<RoleUser> roleAcls = roleUserMapper.selectByExample(example);

        // 2、通过角色 id 获取用户 id
        List<String> user_ids = new ArrayList<>();
        for (RoleUser r: roleAcls){
            user_ids.add(Integer.toString(r.getUser_id()));
        }

        // 3、去除相同的元素
        role_ids.removeAll(user_ids);

        // 4、把角色 id 和 权限点 id 循环插入数据库
        for (int i=0; i<role_ids.size(); i++){
            RoleUser roleUser1 = new RoleUser();
            roleUser1.setId(null);
            roleUser1.setRole_id(roleAcl.getRole_id());
            roleUser1.setUser_id(Integer.valueOf(role_ids.get(i)));
            roleUser1.setOperator("admin");
            roleUser1.setOperate_time(new Date());
            roleUser1.setOperate_ip("127.0.0.1");

            roleUserMapper.insert(roleUser1);
        }
    }
}
